﻿using Knlght.CH.Domain.IRepository;
using Knlght.Domain;
using Knlght.Model.Sys;
using Knlght.Repository;
using System.Collections.Generic;
using Knlght.Repository.Base;

namespace Knlght.Service
{
    public class SettingService : ServiceBase
    {
        public ActionResponse SaveSetting(IEnumerable<Setting> settings)
        {
            return doAction(() =>
            {
                              
                using (var db = new RepositoryBase().BeginTrans())
                {
                    foreach (var setting in settings)
                    {
                        //先删除，再添加
                        db.Delete<Setting>(o => o.Key == setting.Key);
                        db.Insert<Setting>(setting);
                    }
                    db.Commit();
                }
                return true;
            });
        }

        public ListResponse<Setting> GetSettingsBy(string group)
        {
            return GetList<Setting>(() =>
            {
                var rp = getRepository<ISettingRepository>();
                var query = new QueryRequest<Setting>();
                query.Add(o => o.Group == group);
                return rp.GetList(query);
            });
        }
    }
}
